import 'package:dio/dio.dart';
import 'package:yp/config/config.dart';
import 'package:sp_util/sp_util.dart';

class HeaderInterceptor extends InterceptorsWrapper {
  @override
  void onRequest(RequestOptions options, RequestInterceptorHandler handler) {
    super.onRequest(options, handler);
    List specialMethod = [
      'gb.mall.order.submit',
      'order.new.panic.group.hdr.create',
      'ykcloud.order.new.multi.shop.order.createPreSellOrder',
      'ykcloud.order.new.multi.shop.order.createSuperGroupBuyOrder'
    ];
    if (options.data.runtimeType.toString() == 'FormData') {
      options.headers['Accept'] = 'application/json,*/*';
    } else {
      // if (options.data != null &&
      //     (options.data['method'] == 'gb.mall.order.submit' ||
      //         options.data['method'] == 'order.new.panic.group.hdr.create' ||
      //         options.data['method'] ==
      //             'ykcloud.order.new.multi.shop.order.createPreSellOrder' ||
      //         options.data['method'] ==
      //             'ykcloud.order.new.multi.shop.order.createSuperGroupBuyOrder')) {
      if (options.data != null &&
          specialMethod.contains(options.data['method'])) {
        options.headers['content-type'] = 'application/x-www-form-urlencoded';
      } else {
        options.headers['Accept'] = 'application/json,*/*';
      }
    }

    String token = SpUtil.getString(Config.TOKEN);
    // print("request token=$token");
    // if (token.isNotEmpty) {
    //   options.headers["token"] = token;
    // }
  }
}
